﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;

namespace LP2
{
    class ReadXML
    {
        XmlDocument doc;
        public int level;

        public ReadXML()
        {
            doc = new XmlDocument();
        }

        public void ReadXMLLevel()
        {
            int i = 0;
            Boolean flag = false;
            doc.Load("setBackground.xml");
            do
            {
                i++;
                XmlNodeList nivelTag = doc.GetElementsByTagName("Nivel" + i);
                XmlNodeList StateTag = ((XmlElement)nivelTag[0]).GetElementsByTagName("State");
                XmlNodeList LevelTag = ((XmlElement)nivelTag[0]).GetElementsByTagName("Level");

                Console.WriteLine(((XmlElement)StateTag[0]).InnerText);
                if (String.Compare(((XmlElement)StateTag[0]).InnerText, "locked") == 0) flag = true;

            } while (flag==false);

            this.level = i;
            Console.WriteLine(level);

        }

        public void ReadXMLBackground(Background bg)
        {
            //level = 1;//level que debe ser pasado como referencia
            String value;
            
            doc.Load("setBackground.xml");
            XmlNodeList nivelTag = doc.GetElementsByTagName("Nivel"+level);

            XmlNodeList BackgroundTag = ((XmlElement)nivelTag[0]).GetElementsByTagName("Background");
            XmlNodeList ObstaclesTag = ((XmlElement)BackgroundTag[0]).GetElementsByTagName("Obstacle");
            

            foreach (XmlNode ObstacleNode in ObstaclesTag)
            {   
                value = "";
                value += ObstacleNode["posx"].InnerText;
                value += "/";
                value += ObstacleNode["posy"].InnerText;
                value += "/";
                value += ObstacleNode["width"].InnerText;
                value += "/";
                value += ObstacleNode["height"].InnerText;
                value += "/";
                value += ObstacleNode["type"].InnerText;
                bg.loadMatrix(value);
            }

            
        }

        public void ReadXMLInitConfigCreatures(CreaturePropertyConfig cpc)
        {
            String value;
            
            doc.Load("SetInitConfigCreatures.xml");

            //para criaturas
            XmlNodeList EnemyTag = doc.GetElementsByTagName("Enemy");


            foreach (XmlNode nodeEnemy in EnemyTag)
            {
                value = "";
                value += "E";
                value += "/";
                value += nodeEnemy["HP"].InnerText;
                value += "/";
                value += nodeEnemy["MASA"].InnerText;
                cpc.loadCreaturePropertyConfig(value);
            }



            XmlNodeList AllyTag = doc.GetElementsByTagName("Ally");
            foreach (XmlNode nodeAlly in AllyTag)
            {
                value = "";
                value += "A";
                value += "/";
                value += nodeAlly["HP"].InnerText;
                value += "/";
                value += nodeAlly["HIT"].InnerText;
                value += "/";
                value += nodeAlly["MASA"].InnerText;
                cpc.loadCreaturePropertyConfig(value);
            }
            
        }

        public void ReadXMLCreatures(CreaturesList cList)
        {
            doc.Load("setBackground.xml");
            XmlNodeList nivelTag = doc.GetElementsByTagName("Nivel" + level);

            XmlNodeList EnemyTag = ((XmlElement)nivelTag[0]).GetElementsByTagName("Enemy");
            XmlNodeList ETag = ((XmlElement)EnemyTag[0]).GetElementsByTagName("E");


            String value;
            foreach (XmlNode ENode in ETag)
            {   
                value = "";
                value += "E";
                value += "/";
                value += ENode["type"].InnerText;
                value += "/";
                value += ENode["posx"].InnerText;
                value += "/";
                value += ENode["posy"].InnerText;
                cList.loadCreatures(value);
            }


            XmlNodeList AllyTag = ((XmlElement)nivelTag[0]).GetElementsByTagName("Allys");
            XmlNodeList ATag = ((XmlElement)AllyTag[0]).GetElementsByTagName("A");

            foreach (XmlNode ANode in ATag)
            {
                value = "";
                value += "A";
                value += "/";
                value += ANode.InnerText;
                cList.loadCreatures(value);
            }
        }

        public void releaseLevel(int level)
        {
            doc.Load("setBackground.xml");
            XmlNodeList nivelTag = doc.GetElementsByTagName("Nivel" + level);

            XmlNodeList StateTag = ((XmlElement)nivelTag[0]).GetElementsByTagName("State");
            ((XmlNode)StateTag[0]).InnerText = "unlocked";
            doc.Save("setBackground.xml");

        }
    }
}


//XmlDocument doc = new XmlDocument();
//            doc.Load("D:\\build.xml");
//            XmlNode root = doc.DocumentElement;
//            XmlNode myNode = root.SelectSingleNode("descendant::books");
//            myNode.Value = "blabla";
//            doc.Save("D:\\build.xml");